package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.holiday.UserHolidayActivityData;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserHolidayActivityDataDaoImpl extends TempDataObjectDao<UserHolidayActivityData> {


    public  Map<Integer, UserHolidayActivityData> getUserActivityDataMap(long userId, List<Integer> activityIdList){
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, UserHolidayActivityData> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_u_holiday_activity_data where userId = ? and activityId in(" + StringUtils.listToString(activityIdList, ",") + ")";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserHolidayActivityData userHolidayActivityData = parse(rs);
                dataMap.put(userHolidayActivityData.getActivityId(), userHolidayActivityData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(UserHolidayActivityData data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_holiday_activity_data(userId,activityId,hasTimes,useTimes,progressVal,recruitTimes,mergeScore,cats) values "
                + "(?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getActivityId());
            call.setInt(i++, data.getHasTimes());
            call.setInt(i++, data.getUseTimes());
            call.setLong(i++, data.getProgressVal().longValue());
            call.setInt(i++, data.getRecruitTimes());
            call.setLong(i++, data.getMergeScore().longValue());
            call.setString(i++, Arrays.toString(data.getCats()));
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserHolidayActivityData data) {
        PreparedStatement call = null;
        String sql = "update t_u_holiday_activity_data set hasTimes=?,useTimes=?,progressVal=?,recruitTimes=?,mergeScore=?,cats=? where " +
                "activityId=? and userId=? ";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getHasTimes());
            call.setInt(i++, data.getUseTimes());
            call.setLong(i++, data.getProgressVal().longValue());
            call.setInt(i++, data.getRecruitTimes());
            call.setLong(i++, data.getMergeScore().longValue());
            call.setString(i++, Arrays.toString(data.getCats()));
            call.setInt(i++, data.getActivityId());
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserHolidayActivityData parseData(ResultSet rs) throws SQLException {
        UserHolidayActivityData data = new UserHolidayActivityData();
        data.setUserId(rs.getLong("userId"));
        data.setActivityId(rs.getInt("activityId"));
        data.setHasTimes(rs.getInt("hasTimes"));
        data.setUseTimes(rs.getInt("useTimes"));
        data.setProgressVal(BigInteger.valueOf(rs.getLong("progressVal")));
        data.setRecruitTimes(rs.getInt("recruitTimes"));
        data.setMergeScore(BigInteger.valueOf(rs.getLong("mergeScore")));
        String value = rs.getString("cats");
        if(!StringUtils.isNullOrEmpty(value)) {
            data.setCats(JSONObject.parseObject(value, int[].class));
        }
        return data;
    }
}
